package chapter_04_singleton.p5_mapSingleton;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by CharloYao on 2017/3/23.
 * 利用缓存来实现单例；不考虑线程安全；
 * 这个模式不太好，不要用******
 */
public class CacheSingleton {

    private CacheSingleton(){};

    private static final String KEY = "ONE";

    private static Map<String,CacheSingleton> map = new HashMap<>();

    public static CacheSingleton getInstance(){
        //先从缓存中取
        CacheSingleton instance = map.get(KEY);
        //没有则新建
        if(instance == null) {
            instance = new CacheSingleton();
            map.put(KEY, instance);
        }
        //返回实例
        return instance;
    }
}
